package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public void saveWithDishFlavor(DishDto dishDto) {
        //保存菜品基本信息到菜品表dish
        this.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        //将菜品ID保存到菜品口味中 (对应dish_flavor表中的dish_id列)
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品的基本信息和口味信息 用于回显需要修改的菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithDishFlavor(Long id) {
        // 从dish表查询菜品基本信息,并拷贝给dishDto
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        // 从dish_flavor表中查询口味信息, 并赋值给dishDto.
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    public void updateWithDishFlavor(DishDto dishDto) {
        //修改菜品表
        this.updateById(dishDto);
        //修改菜品口味表
        //1.清理
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());

        dishFlavorService.remove(queryWrapper);
        //2.重新添加
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 给每一项添加dishId,
        flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本数据
        this.save(dishDto);
        //保存菜品口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //将菜品ID保存到菜品口味中 (对应dish_flavor表中的dish_id列)
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        //保存菜品口味信息
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //获取并拷贝菜品基本数据至dishDto中
        DishDto dishDto = new DishDto();
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishDto);
        //获取菜品口味信息并添加到dishDto中.
        //根据菜品id获取菜品口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //先在dish表中更新菜品的基本信息
        this.updateById(dishDto);
        //在dish_flavor表中删除之前的口味信息, 重新添加新的口味信息.
        // 删除相同dish_id的口味表中的字段.
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //获取dishDto中的flavors数据并给每一条数据添加dish_id,因为没有dish_id则不知道口味数据具体是哪一种菜品的.
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public void deleteLogic(List<Long> ids) {
        // 1. 判断集合中的id是否全部存在.
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            queryWrapper.eq(Dish::getId, id);
            // 1.2 如果不存在, 抛出CustomException提示ID不存在.
            Dish dish = this.getOne(queryWrapper);
            if (dish == null){
                throw new CustomException("菜品不存在");
            }
            // 1.1 如果全部存在, 执行removeByIds()
            this.removeByIds(ids);
            // (是否使用逻辑删除?)
        }
    }
}
